<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li>
                  <a title="全部" href="#">全部</a>
                </li>
                <li
                  v-for="(item ,index) in subjectNestedList"
                  :key="index"
                  :class="{active:oneIndex === index}"
                >
                  <a :title="item.title" @click="searchOne(item.id,index)" href="#">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <!-- 二级分类 -->
              <ul class="clearfix">
                <li
                  v-for="(item, index) in subSubjectList"
                  :key="index"
                  :class="{active:twoIndex === index}"
                >
                  <a :title="item.title" href="#" @click="searchTwo(item.id,index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange':buyCountSort!=''}">
                <a title="销量" href="javascript:void(0);" @click="searchBuyCount()">
                  销量
                  <span :class="{hide:buyCountSort==''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange':gmtCreateSort!=''}">
                <a title="最新" href="javascript:void(0);" @click="searchGmtCreate()">
                  最新
                  <span :class="{hide:gmtCreateSort==''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange':priceSort!=''}">
                <a title="价格" href="javascript:void(0);" @click="searchPrice()">
                  价格&nbsp;
                  <span :class="{hide:priceSort==''}">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section v-if="data.total == 0 " class="no-data-wrap">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->

          <article v-if="data.total > 0" class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="item in data.items" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="item.cover" class="img-responsive" :alt="item.title" />
                    <div class="cc-mask">
                      <a  :href="'/course/'+item.id"  :title="item.title" class="comm-btn c-btn-1">{{item.title}}</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a
                      :href="'/course/'+item.id"
                      :title="item.title"
                      class="course-title fsize18 c-333"
                    >{{item.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green" v-if="Number(item.price) === 0">
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <!-- <span class="fr jgTag bg-green" v-else-if="Number(item.price) > 0">
                      <i class="c-fff fsize12 f-fA">收费</i>
                    </span>-->
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{item.buyCount}}人学习</i>
                      |
                      <i class="c-999 f-fA">9634评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <!-- <div>
          <div class="paging">
            <a class="undisable" title>首</a>
            <a id="backpage" class="undisable" href="#" title>&lt;</a>
            <a href="#" title class="current undisable">1</a>
            <a href="#" title>2</a>
            <a id="nextpage" href="#" title>&gt;</a>
            <a href="#" title>末</a>
            <div class="clear"></div>
          </div>
        </div>-->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{undisable: !data.hasPrevious}"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)"
            >首页</a>
            <a
              :class="{undisable: !data.hasPrevious}"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(data.current-1)"
            >&lt;</a>
            <a
              v-for="page in data.pages"
              :key="page"
              :class="{current: data.current == page, undisable: data.current == page}"
              :title="'第'+page+'页'"
              href="#"
              @click.prevent="gotoPage(page)"
            >{{ page }}</a>
            <a
              :class="{undisable: !data.hasNext}"
              href="#"
              title="后一页"
              @click.prevent="gotoPage(data.current+1)"
            >&gt;</a>
            <a
              :class="{undisable: !data.hasNext}"
              href="#"
              title="末页"
              @click.prevent="gotoPage(data.pages)"
            >末页</a>
            <div class="clear" />
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
// 引入 js
import courseApi from "@/api/course";

export default {
  data() {
    return {
      page: 1, //当前页
      data: {}, // 查询课程列表
      subjectNestedList: [], // 一级分类列表
      subSubjectList: [], // 二级分类列表
      searchObj: {}, // 查询表单对象
      oneIndex: -1, // 一级分类选中
      twoIndex: -1, // 二级分类选中
      buyCountSort: "",
      gmtCreateSort: "",
      priceSort: "",
    };
  },
  created() {
    // 1. 查询第一页数据
    this.initCourseFirst();
    // 2. 查询一二级分类
    this.initSubject();
  },
  methods: {
    // 1.课程第一次初始化
    initCourseFirst() {
      courseApi.getCourceList(1, 8, this.searchObj).then((response) => {
        // console.log(response);
        this.data = response.data.data;
      });
    },
    // 2.查询所有的一级、二级分类
    initSubject() {
      courseApi.getAllSubject().then((response) => {
        console.log(response.data.data.treeList);
        this.subjectNestedList = response.data.data.treeList;
      });
    },
    //3.分页切换
    gotoPage(page) {
      courseApi.getCourceList(page, 8, this.searchObj).then((response) => {
        console.log(response);
        this.data = response.data.data;
      });
    },
    // 4.根据一级分类查询二级分类
    searchOne(subjectParentId, index) {
      // 设置当前oneindex的值为 当前选中的 一级分类对应的 index
      // 显示 点击一级分类  加入 active选中效果
      this.oneIndex = index;

      //初始化 值
      this.twoIndex = -1;
      this.searchObj.subjectId = "";
      this.subSubjectList = [];

      // 将点击的一级分类id 赋值给当前查询对象
      this.searchObj.subjectParentId = subjectParentId;
      // 再次调用 查询方法 ,默认从第一页查询
      this.gotoPage(this.page);

      // 根据传入 一级分类id和所有的一级分类循环比较id是否一样
      // 如何相同，  获取改一级分类中的二级分类获取
      for (let i = 0; i < this.subjectNestedList.length; i++) {
        // 获取每一个一级分类
        var oneSubject = this.subjectNestedList[i];
        // 比较id 是否相同
        if (subjectParentId === oneSubject.id) {
          // 将当前对应的一级分类的所有二级分类赋值给当前 subSubjectList
          this.subSubjectList = oneSubject.children;
        }
      }
    },
    // 5. 二级分类点击事件
    searchTwo(subjectId, index) {
      //将传入的 index 赋值 给当前 twoIndex，显示选中效果
      this.twoIndex = index;
      // 增加条件查询， 二级分类id
      this.searchObj.subjectId = subjectId;
      // 调用查询方法
      this.gotoPage(1);
    },
    // 6. 根据销量排序
    searchBuyCount() {
      this.buyCountSort = "1";
      this.gmtCreateSort = "";
      this.priceSort = "";

      // 将查询条件赋值
      this.searchObj.buyCountSort = this.buyCountSort;
      this.searchObj.gmtCreateSort = this.gmtCreateSort;
      this.searchObj.priceSort = this.priceSort;

      //调用 查询方法
      this.gotoPage(1);
    },
    // 7.根据 最新时间 排序
    searchGmtCreate() {
      this.buyCountSort = "";
      this.gmtCreateSort = "1";
      this.priceSort = "";

      // 将查询条件赋值
      this.searchObj.buyCountSort = this.buyCountSort;
      this.searchObj.gmtCreateSort = this.gmtCreateSort;
      this.searchObj.priceSort = this.priceSort;

      //调用 查询方法
      this.gotoPage(1);
    },
    // 8. 根据 价格排序
    searchPrice() {
      this.buyCountSort = "";
      this.gmtCreateSort = "";
      this.priceSort = "1";

      // 将查询条件赋值
      this.searchObj.buyCountSort = this.buyCountSort;
      this.searchObj.gmtCreateSort = this.gmtCreateSort;
      this.searchObj.priceSort = this.priceSort;

      //调用 查询方法
      this.gotoPage(1);
    },
  },
};
</script>
<style scoped>
.active {
  background: #bdbdbd;
}
.hide {
  display: none;
}
.show {
  display: block;
}
</style>